package org.gl.customview.listview;

import org.gl.android.base.R;

import android.content.Context;
import android.database.DataSetObserver;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.graphics.Canvas;
import android.graphics.Paint;
import android.util.AttributeSet;
import android.widget.ListAdapter;
import android.widget.ListView;

/**
 * draw a image. if the listview is no item,
 * 
 * @author gl
 * 
 */
public class ListViewEmpty extends ListView {

	private boolean isEmpty = false;
	private Paint paint;
	private boolean isStop3;
	private int steep30;
	private int steep31;
	private Bitmap bitmap;
	private int viewW = 120;
	private DataSetObserver mDataSetObserver;

	public ListViewEmpty(Context context, AttributeSet attrs) {
		super(context, attrs);
		init();
	}

	public ListViewEmpty(Context context) {
		super(context);
		init();
	}

	private void init() {
		paint = new Paint();
		paint.setColor(0xff50f6a3);
		bitmap = BitmapFactory.decodeResource(getResources(),
				R.drawable.ic_empty);
		viewW = bitmap.getWidth();
		mDataSetObserver = new DataSetObserver() {
			@Override
			public void onChanged() {
				super.onChanged();
				isEmpty();
			}
		};
	}

	@Override
	protected void onAttachedToWindow() {
		super.onAttachedToWindow();
		isEmpty();
	}

	@Override
	public void setAdapter(ListAdapter adapter) {
		super.setAdapter(adapter);
		adapter.registerDataSetObserver(mDataSetObserver);
		isEmpty();
	}

	@Override
	protected void onDetachedFromWindow() {
		super.onDetachedFromWindow();
		if (getAdapter() != null && mDataSetObserver != null) {
			try {
				getAdapter().unregisterDataSetObserver(mDataSetObserver);
				mDataSetObserver= null;
			} catch (Exception e) {
				e.printStackTrace();
			}
		}
		if (bitmap != null && !bitmap.isRecycled()) {
			bitmap.recycle();
			bitmap=null;
		}
	}

	@Override
	protected void onDraw(Canvas canvas) {
		super.onDraw(canvas);
		if (isEmpty) {
			// canvas.drawCircle(getWidth() / 2, getHeight() / 2, 50, paint);
			drawClip(canvas);
		}
	}

	private void isEmpty() {
		if (getAdapter() == null || getAdapter().getCount() <= 0) {
			isEmpty = true;
			resetClip();
		} else {
			isEmpty = false;
			invalidate();
		}
	}

	private void resetClip() {
		steep30 = 0;
		steep31 = 0;
		isStop3 = false;
	}

	float center_x = 0;
	float center_y = 0;

	// 建立遮罩效果动画
	private void drawClip(Canvas canvas) {
		// 动画完成时，把整张图片显示出来
		if (isStop3) {
			canvas.drawBitmap(bitmap, center_x - bitmap.getWidth() / 2,
					center_y - bitmap.getHeight() / 2, null);
			return;
		} else {
			invalidate();
		}
		// 当进行到当完成，将动画停止，但别忘了把背景图片显示出来
		if (steep30 >= viewW && viewW != 0) {

			isStop3 = true;
			canvas.drawBitmap(bitmap, center_x - bitmap.getWidth() / 2,
					center_y - bitmap.getHeight() / 2, null);
			return;
		}
		// 20开始
		for (int i = 0; i <= viewW; i = i + 40) {
			center_x = getLeft() + getWidth() / 2;
			center_y = getTop() + getHeight() / 2;
			// 创建从左到右的遮罩动画
			clip(canvas, bitmap, steep30, 20,
					(int) (center_x - bitmap.getWidth() / 2), (int) (center_y
							- bitmap.getHeight() / 2 + i));
			// 创建从右到左的遮罩动画
			clip(canvas, bitmap, steep31, 20,
					(int) (center_x - bitmap.getWidth() / 2 + viewW - steep31),
					(int) (center_y - bitmap.getHeight() / 2 + i + 20));
		}
		// 运行一次遮罩动画前进行距离
		steep30 += 5;
		steep31 += 5;
	}

	// 进行图片裁剪
	private void clip(Canvas canvas, Bitmap bit, int w, int h, int posX,
			int posY) {
		canvas.save();
		canvas.clipRect(posX, posY, posX + w, posY + h);
		canvas.drawBitmap(bit, center_x - bitmap.getWidth() / 2, center_y
				- bitmap.getHeight() / 2, null);
		canvas.restore();

	}
}
